#!/bin/csh 
# Author: Jurgen F. Doreleijers @ Wed Feb 23 15:59:44 CST 2000
#
# TASK: A wrap around the script: Qwhatif3.csh enabling multiple models/files
# USE:  WI_rename.csh t|f t|f xxxx.pdb
# EXAMPLE: WI_rename.csh t f t ../PDB/1brv.pdb
#
# NOTES:- pdb file can be specified with path name but the output files
#         are (over)written in the current working directory.
#       - assumes the name of the pdb files have a .pdb extension
#       - assumes whatif is installed and the topology file location, defined
#         below, exists.
# OPTIONS
#       - see usage echoed when no arguments are given.
#
# Change the file location of the topology file containing hydrogens if needed
# /home is actually /share/linux/
set topology_file = "$LH/WHATIF/run/TOPOLOGY.FIL"

# No need to change things below this line
###############################################################################
if ($#argv != 4 ) then
 goto usage
endif

# do_logs
if ( !(( $1 == "t" ) || ( $1 == "f") ) ) then
 goto usage
endif

# add_protons
if ( !(( $2 == "t" ) || ( $2 == "f") ) ) then
 goto usage
endif

#do_write
if ( !(( $3 == "t" ) || ( $3 == "f") ) ) then
 goto usage
endif

# change the variable do_logs to anything else than t to get no logs.
set do_logs     = $1

# do we need protons added? Set it to anything else than t to not add them.
set add_protons = $2

# do we need a pdb output file?
set do_write    = $3

set out = whatif.script
# Get the bare entry code for the pdb file.
set x   = $4:t:gr
# Get the directory the pdb file is in.
set dir = $4:h

# Zip it where equal
if ( "$dir" == "$4" ) then
 set dir = ""
endif

set log_file = $x"_wi".log
# Delete the log file
if ( -e $log_file ) then
  \rm -f $log_file
endif

# Get a copy of a proton topology file
ln -f -s $topology_file TOPOLOGY.FIL

# Delete the wi script file
if ( -e $out ) then
  \rm -f $out
endif

# Check if input file exists
if ( ! -e $4 ) then
 echo "ERROR: Aborting, file does not exist: $4"
 exit(2)
endif

echo "Trying to separate models from the PDB file"
splitpdb $4 > /dev/null
set NOSPLIT = $status

# How many models are we handeling
if ( $NOSPLIT ) then
    # Single file
    
    # Is the file local already
    if ( $dir == "" ) then
    # Yes, local
       set was_copy = "f"
    else
    # No, not local
       if ( -e $x.pdb ) then
#         echo "the found dir is: $dir"
         echo "ERROR: Please delete local file $x.pdb first"
         exit(3)
       endif
#       echo "Make a temporary local copy of $x.pdb"
       set was_copy = "t"
       ln -s $4 $x.pdb
    endif
    
    set list = $x.pdb
else
    # Multiple file
    set list = ( `\ls $x"_"[0-9][0-9][0-9].pdb` )

    if ( ! $#list ) then
      echo "ERROR: No pdb files anyway (1)"
      exit (4)
    endif
endif


if ( ! $#list ) then
  echo "ERROR No: pdb files anyway (2)"
  exit (5)
endif

set number_models = $#list

echo "Will transform $#list models"

foreach pdb_file ( $list )
#   echo "WHAT IF will process PDB file " $pdb_file

   set z = $pdb_file:t:gr
   
   # Removing files so WI will not ask for confirmation
   set delete_list = $z"_wi.pdb"
   
   if ( $do_logs == "t" ) then
     set delete_list = ( $delete_list $z.namchk.log $z.lista.log )
   endif

   foreach file ( $delete_list )
     if ( -e $file ) then
      echo "Deleting file: $file"
      \rm $file
     endif
   end

   # Generate WI script

   # Set WI options
   echo "# Truncating errors in a PDBOUT table"                        >> $out
   echo "SETWIF 593 100"                                               >> $out
   echo "# Skipping pseudos"                                           >> $out
   echo "SETWIF 1505 1"                                                >> $out
   echo "# Read all models"                                            >> $out
   echo "SETWIF 847 1"                                                 >> $out
   echo "# Not adding C-terminal O if missing"                         >> $out
   echo "SETWIF 1071 1"                                                >> $out
   echo "# We have an NMR structure"                                   >> $out
   echo "SETWIF 1503 1"                                                >> $out
   echo "# IUPAC nomenclature"                                         >> $out
   echo "SETWIF 142 1"                                                 >> $out

   # Read the one model
   echo "getmol"                                                       >> $out
   echo "$pdb_file"                                                    >> $out
   echo "xxx"                                                          >> $out
   echo ""                                                             >> $out

   ## Add hydrogens if missing
   if ( $add_protons == "t" ) then
      echo "# Adding hydrogens"                                        >> $out
      echo "%ADDHYD TOT 0"                                             >> $out
      echo ""                                                          >> $out
   endif
   
   # Start a log file for the name checking routine
   if ( $do_logs == "t" ) then
     echo "dolog"                                                      >> $out
     echo "$z.namchk.log"                                              >> $out
     echo "name check of $pdb_file"                                    >> $out
     echo "0"                                                          >> $out
     echo "time"                                                       >> $out
   endif

   # Do the actual name check
   echo "%namchk"                                                      >> $out
   # Finish the log file

   if ( $do_logs == "t" ) then
     echo "nolog"                                                      >> $out
   endif

   echo ""                                                             >> $out

   # Write the new PDB file
   if ( $do_write == "t" ) then
      echo "%makmol"                                                      >> $out
      echo "/dev/null"                                                    >> $out
      echo "$z""_wi.pdb"                                                  >> $out
      echo "WHAT IF"                                                      >> $out
      echo "WHAT IF corrected nomenclature"                               >> $out
      echo "WHAT IF"                                                      >> $out
      echo "0"                                                            >> $out
      echo "tot"                                                          >> $out
      echo "0"                                                            >> $out
      echo ""                                                             >> $out
   endif
   
   # Start a log file for the lista output
   if ( $do_logs == "t" ) then
      echo "dolog"                                                     >> $out
      echo "$z.lista.log"                                              >> $out
      echo "lista of $pdb_file"                                        >> $out
      echo "0"                                                         >> $out
      echo "time"                                                      >> $out
      echo "lista all"                                                 >> $out
      echo "nolog"                                                     >> $out
      echo ""                                                          >> $out
   endif

   # Initialize the soup
   echo "%inisou"                                                      >> $out
   echo ""                                                             >> $out
   
end

echo "fullstop y"                                                      >> $out

# Run whatif with the script
whatif script $out > $log_file


# Delete some junk
\rm check.db pdbout.txt pdbout.tex WHATIF.FIG TOPOLOGY.FIL $out >& /dev/null

# Strip the model lines that WI writes out if the structure is multimeric
#foreach pdb_file ( $list )
#   set z = $pdb_file:t:gr
#   gawk '/^MODEL/{next}\
#         /^ENDMDL/{next}\
#         {print}' $z"_wi.pdb" > tempje
#   \mv tempje $z"_wi.pdb"
#end
#echo "Deleting the model records. "

# Join the resulting files to xxxx_wi.pdb
if ( $NOSPLIT ) then
  if ( $was_copy == "t" ) then  
#     echo "Removing local copy of the file $x.pdb"
     \rm -f $x.pdb
  endif
else
  # Delete the original list of files splitted
  \rm -f $list

   if ( $do_write == "t" ) then
      # Get the new list of files generated.
      set list = ( `\ls $x"_"[0-9][0-9][0-9]"_wi".pdb` )

      if ( ! $#list ) then
        echo "ERROR: No resulting pdb files anyway"
        exit (6)
      endif

      if ( $#list != $number_models ) then
        echo "ERROR: different number of resulting pdb files $#list was $number_models"
        exit (7)
      endif

      echo "Concatenating the individual models"
      $Q/extras/joinpdb -o $x"_wi".pdb $list > /dev/null

      # Delete the new list of files generated
      \rm -f $list
   endif
   
endif

if ( $do_logs == "f" ) then
 \rm $log_file
endif

exit(0)



#####################
# usage block
#####################
usage:
    echo 'Usage: WI_rename.csh t|f t|f t|f pdb_file'
    echo ""
    echo "ERROR: Please supply three arguments;"
    echo "1) t or f for writting a log file"
    echo "2) t or f for adding missing protons"
    echo "3) t or f for writting a pdb file"
    echo "3) the pdb file name with or without path"
    exit (1)
